<?php   if (!defined('BASEPATH')) exit('No direct script access allowed');


class Ricerche_model extends CI_Model {
	
	private $params = array();
    function __construct()
    {
        parent::__construct();
		
    }

    function get_columns()
	{
		$sql = "SELECT column_name from information_schema.columns where
table_name='" . $this->db->dbprefix('ricerche') . "'";
		$query = $this->db->query($sql);
		
		return $query->result_array();
	}
    
	function get_total($params = array())
	{
		
		if(empty($this->params))
		{
			$default_params = array
			(
				'where' => null,
				'group_by' => array()
			);
			
			foreach ($default_params as $key => $value)
			{
				$this->params[$key] = (!empty($params[$key]))? $params[$key]: $default_params[$key];
			}
		}
        
		
		$this->db->select('*');
		$this->db->from('ricerche as r');
		$this->db->where( $this->params['where']);
        //var_dump($this->params);
        $query = $this->db->get();
		$nb = $query->num_rows();
		return $nb;
	}
		

	
	function get_list($params = array())
	{
        
		$default_params = array
		(
			'select' => 'r.*',
			'where' => null,
			'limit' => null,
			'start' => 0,
			'order_by' => 'r.id DESC',
		);
        
		foreach ($default_params as $key => $value)
		{
			$params[$key] = (isset($params[$key]))? $params[$key]: $default_params[$key];
		}
        $this->params = $params;
		$this->db->select($params['select']);
		$this->db->from('ricerche as r');
		if(!is_null($params['where']))
		{
			$this->db->where($params['where']);
		}
		if(!is_null($params['limit']))
		{
			$this->db->limit($params['limit'], $params['start']);
		}
		$this->db->order_by($params['order_by']);
		
		
        $query = $this->db->get();
        
		
		return $query->result_array();
	}

	function delete($id)
	{
		if(is_array($id))
		{
			$this->db->where($id);
		}
		else
		{
			$this->db->where('id', $id);
		}
		$this->db->delete('ricerche');
	}
	
    function save($data)
    {
        
   		if(!isset($data['id']) || !$data['id'])
        {
            $query = $this->db->query("SELECT max(id) as id FROM ricerche");
            $row = $query->row_array();
            $data['id'] = $row['id']+1;
			
            $this->db->insert('ricerche', $data);
        }
        else
        {
            $this->db->where('id', $data['id']);
			
            $this->db->update('ricerche', $data);
        }
		return $data['id'];

    }
    
}